package com.hystrix.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;

import java.util.Random;

/**
 * 测试缓存
 */
public class CacheCommand extends HystrixCommand<String> {


    private String key ;

    public CacheCommand(String key) {
        super(
            Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                    .andCommandKey(HystrixCommandKey.Factory.asKey("MyCommandKey"))
        );
        this.key = key;
    }

    @Override
    protected String run() throws Exception {
        System.out.println(" CacheCommand-------执行命令");
        Random r = new Random();
        //故意设置随机数返回，测试是否读取缓存
        return r.nextInt()+" ";
    }

    //重写方法，开启缓存
    @Override
    protected String getCacheKey() {
        return this.key;
    }
}
